package com.iseastar.guojiang.newcabinet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.NewParcelBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.model.TaskStageBean;
import com.iseastar.guojiang.transfer.ParcelTransferListActivity;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.utils.Android;
import droid.frame.utils.lang.Str;

/**
 * Created by joker on 2017/12/19.
 * 任务阶段
 * 该类分为 上门取件，回仓交接，快递柜取件，驿站揽收
 * 1.只有上门取件才有第二步的操作，其他的都没有
 * 2.回仓交接的颜色值和图片和其他的状态颜色值都不一样
 */
public class TaskStageFragment extends BaseFragment implements View.OnClickListener {

    private TextView mTaskTypeTV;//任务类型,分为：上门取件，回仓交接，快递柜取件，驿站揽收
    private TextView mSurplusTimeTV;//剩余时间
    private TextView mAddressNameTV;//地址名称
    private TextView mAddressDetailsTV;//详细地址
    private TextView mRemarkTV;//备注
    private View mContractLayout;
    private TextView mSenderNameTV;//寄件人姓名
    private ImageView mSenderPhoneIV;//寄件人电话
    private ImageView mSenderStationIV;//寄件人是否驿站
    private TextView mTransferTimeTV;//回仓交接时间 如：17:30-18:30
    private TextView mTransferTimeTipsTV;//回仓交接时间剩余时间提示 如：（32分钟后停止交接）
    private TextView mAllTransferTV;//查看所有交接点
    /**
     * 第一步相关布局
     */
    private LinearLayout mFirstStepLL;//第一步的布局
    private ImageView mFirstStepImageIV;//第一步步骤图片展示
    private TextView mFirstStepNameTV;//第一步的名称
    private TextView mFirstStepTipTV;//第一步提示
    private TextView mFirstStepContentTV;//第一步显示的内容
    private TextView mFirstStepRightTV;//第一步右边按钮操作
    /**
     * 第二步相关布局
     */
    private LinearLayout mSecondStepLL;//第二步的布局
    private ImageView mSecondStepImageIV;//第二步步骤的图片展示
    private TextView mSecondStepNameTV;//第二步的名称
    private TextView mSecondStepTipTV;//第二步提示
    private TextView mSecondStepContentTV;//第二步显示的内容
    private TextView mSecondStepRightTV1;//第二步右边按钮操作
    private TextView mSecondStepRightTV2;//第二步右边按钮操作

    private ServiceTaskBean serviceTaskBean = null;
    private TaskStageBean stageBean = null;
    private CountDownTimer timer = null;
    private boolean isCallPhone = false;//是否上门拨打联系人电话
    // 定位相关，传递位置
    private BDLocation mBdLocation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            serviceTaskBean = (ServiceTaskBean) bundle.getSerializable("item");
            showLoadingDialog(null);
            AppHttp.getInstance().getTaskStageDetail(serviceTaskBean.getTaskId());
            //快递柜揽收需要坐标位置
            if (Const.CourierTaskStatus.cabinetTask == serviceTaskBean.getTaskType()) {
                Intents.startBaiduLocation(getActivityContext(), MsgID.courier_task_stage_location_data);
            }
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.fragment_task_stage);
        super.findViewById();
        mTaskTypeTV = (TextView) findViewById(R.id.task_type_tv);
        mSurplusTimeTV = (TextView) findViewById(R.id.surplus_time_tv);
        mAddressNameTV = (TextView) findViewById(R.id.address_name_tv);
        mAddressDetailsTV = (TextView) findViewById(R.id.address_details_tv);
        mRemarkTV = (TextView) findViewById(R.id.remark_tv);
        mContractLayout = findViewById(R.id.contract_layout_ll);
        mSenderNameTV = (TextView) findViewById(R.id.sender_name_tv);
        mSenderPhoneIV = (ImageView) findViewById(R.id.sender_phone_tv);
        mSenderPhoneIV.setOnClickListener(this);
        mSenderStationIV = (ImageView) findViewById(R.id.sender_station_tv);
        mSenderStationIV.setOnClickListener(this);
        mTransferTimeTV = (TextView) findViewById(R.id.transfer_time_tv);
        mTransferTimeTipsTV = (TextView) findViewById(R.id.transfer_time_tip_tv);
        mAllTransferTV = (TextView) findViewById(R.id.all_transfer_tv);
        mAllTransferTV.setOnClickListener(this);
        findViewById(R.id.map_tv).setOnClickListener(this);
        findViewById(R.id.navigation_tv).setOnClickListener(this);
        //******************************** 第一步布局*******************************************//
        mFirstStepLL = (LinearLayout) findViewById(R.id.step_first_ll);
        mFirstStepImageIV = (ImageView) findViewById(R.id.first_step_image_iv);
        mFirstStepNameTV = (TextView) findViewById(R.id.first_step_name_tv);
        mFirstStepTipTV = (TextView) findViewById(R.id.first_step_tip_tv);
        mFirstStepContentTV = (TextView) findViewById(R.id.first_step_content_tv);
        mFirstStepRightTV = (TextView) findViewById(R.id.first_step_right_tv);
        mFirstStepRightTV.setOnClickListener(this);
        //********************************第二步布局*******************************************/
        mSecondStepLL = (LinearLayout) findViewById(R.id.step_second_ll);
        mSecondStepImageIV = (ImageView) findViewById(R.id.second_step_image_iv);
        mSecondStepNameTV = (TextView) findViewById(R.id.second_step_name_tv);
        mSecondStepTipTV = (TextView) findViewById(R.id.second_step_tip_tv);
        mSecondStepContentTV = (TextView) findViewById(R.id.second_step_content_tv);
        mSecondStepRightTV1 = (TextView) findViewById(R.id.scan_qrcode_tv);
        mSecondStepRightTV1.setOnClickListener(this);//扫描二维码
        mSecondStepRightTV2 = (TextView) findViewById(R.id.confirm_send_code_tv);
        mSecondStepRightTV2.setOnClickListener(this);//验证寄件码

        updateView();
    }

    /**
     * 刷新布局的所有数据
     *
     * @param bean
     */
    public void refreshDataToView(ServiceTaskBean bean) {
        this.serviceTaskBean = bean;
        updateView();
        showLoadingDialog(null);
        AppHttp.getInstance().getTaskStageDetail(serviceTaskBean.getTaskId());
    }

    private void updateView() {
        if (serviceTaskBean == null) {
            return;
        }
        if (Const.CourierTaskStatus.transferTask == serviceTaskBean.getTaskType()) {//回仓
            mSecondStepLL.setVisibility(View.GONE);
            mContractLayout.setVisibility(View.VISIBLE);
            //回仓交接时间布局
            findViewById(R.id.transfer_time_ll).setVisibility(View.VISIBLE);
            //默认交接地点
            findViewById(R.id.default_transfer_tv).setVisibility(View.VISIBLE);
            findViewById(R.id.transfer_all_ll).setVisibility(View.VISIBLE);
            mTransferTimeTV.setText(serviceTaskBean.getTransStart() + "-" + serviceTaskBean.getTransEnd());
            if (isEmpty(serviceTaskBean.getTimeMessage())) {
                mTransferTimeTipsTV.setText("");
            } else {
                mTransferTimeTipsTV.setText("（" + serviceTaskBean.getTimeMessage() + "）");
            }
            mFirstStepImageIV.setImageResource(R.drawable.task_first_unfinish_blue);
            mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.app_theme_color1));
            mSenderPhoneIV.setImageResource(R.drawable.task_phone_icon);
            mTaskTypeTV.setText("回仓交接");
            mTaskTypeTV.setBackgroundResource(R.drawable.parcel_to_step_5c7cd8_bg);
            mSurplusTimeTV.setVisibility(View.GONE);
            mAllTransferTV.setText("查看所有交接点（最近交接点" + serviceTaskBean.getDistance1() + "米）");
            mAddressNameTV.setText(serviceTaskBean.getWareHouseName());
            mAddressDetailsTV.setText(serviceTaskBean.getAddress());
            mRemarkTV.setText("温馨提示：减少回仓次数，可有效减少路程耗费");
            mRemarkTV.setTextColor(getResources().getColor(R.color.app_theme_color1));
            mSenderNameTV.setText("联系人：" + serviceTaskBean.getContactName());
            mFirstStepRightTV.setText("出示二维码");
            mFirstStepNameTV.setText("包裹回仓");
            mFirstStepTipTV.setVisibility(View.GONE);
            mFirstStepContentTV.setText("出示火箭侠二维码，交接包裹");
        } else {
            //回仓的交接时间
            findViewById(R.id.transfer_time_ll).setVisibility(View.GONE);
            //默认交接地点
            findViewById(R.id.default_transfer_tv).setVisibility(View.GONE);
            //查看所有交接点
            findViewById(R.id.transfer_all_ll).setVisibility(View.GONE);
            mAddressNameTV.setText(serviceTaskBean.getBuildName());
            mAddressDetailsTV.setText(serviceTaskBean.getAddress());
            mSurplusTimeTV.setVisibility(View.VISIBLE);
            mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.app_color_ef4040));
            mTaskTypeTV.setBackgroundResource(R.drawable.parcel_to_step_bg);
            mSenderPhoneIV.setImageResource(R.drawable.task_recivier_phone_icon);
            if (isEmpty(serviceTaskBean.getRemark())) {
                mRemarkTV.setVisibility(View.INVISIBLE);
            } else {
                mRemarkTV.setVisibility(View.VISIBLE);
                mRemarkTV.setText("备注：" + serviceTaskBean.getRemark());
                mRemarkTV.setTextColor(getResources().getColor(R.color.app_color_ef4040));
            }
            if (Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType()) {//驿站
                mSecondStepLL.setVisibility(View.GONE);
                mContractLayout.setVisibility(View.VISIBLE);
                mSenderNameTV.setVisibility(View.VISIBLE);
                mTaskTypeTV.setText("驿站揽收");
                mSenderNameTV.setText("商户：" + serviceTaskBean.getContactName());
                mFirstStepRightTV.setText("扫描二维码");
                mFirstStepNameTV.setText("揽收包裹");
                mFirstStepTipTV.setVisibility(View.GONE);
                mFirstStepContentTV.setText("扫描驿站二维码，揽收包裹");
            } else if (Const.CourierTaskStatus.cabinetTask == serviceTaskBean.getTaskType()) {//快递柜
                mSecondStepLL.setVisibility(View.GONE);
                mContractLayout.setVisibility(View.GONE);
                mTaskTypeTV.setText("快递柜取件");
                mFirstStepRightTV.setText("揽收");
                mFirstStepNameTV.setText("验收包裹");
                mFirstStepTipTV.setVisibility(View.GONE);
                mFirstStepContentTV.setText("在距快递柜200米内才可点击");
            } else {//上门
                //上门是否显示驿站标识
                if (Str.isNotEmpty(serviceTaskBean.getStationName())) {
                    mSenderStationIV.setVisibility(View.VISIBLE);
                } else {
                    mSenderStationIV.setVisibility(View.GONE);
                }
                mContractLayout.setVisibility(View.VISIBLE);
                mSenderNameTV.setVisibility(View.VISIBLE);
                mSecondStepLL.setVisibility(View.VISIBLE);
                mFirstStepTipTV.setVisibility(View.VISIBLE);
                mSenderNameTV.setText("寄件人：" + serviceTaskBean.getContactName());
                mTaskTypeTV.setText("上门取件");
                mFirstStepRightTV.setText("联系寄件人");
                mFirstStepNameTV.setText("上门确认");
                mFirstStepTipTV.setText("(第一步）");
                mFirstStepContentTV.setText("请在通话中确认寄件人地址与时间");
                mSecondStepNameTV.setText("验收包裹");
                mSecondStepTipTV.setText("(第二步）");
                mSecondStepContentTV.setText("根据实际情况选择验证方式");
            }
            long overTime = serviceTaskBean.getOverTime();
            long startTime = serviceTaskBean.getCreateTime();
            if (System.currentTimeMillis() > overTime) {
                mSurplusTimeTV.setText("已超时");
            } else {
                remaintimeCountdown(mSurplusTimeTV, overTime - startTime);
                timer = new CountDownTimer(overTime - startTime, 60 * 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        remaintimeCountdown(mSurplusTimeTV, millisUntilFinished);
                    }

                    @Override
                    public void onFinish() {
                        mSurplusTimeTV.setText("已超时");
                    }
                };
                timer.start();
            }
        }
    }

    /**
     * 根据步骤展示相应的界面
     *
     * @param bean
     */
    private void updateViewForStep(TaskStageBean bean) {
        if (Const.CourierTaskStatus.transferTask == serviceTaskBean.getTaskType()) {//回仓
            mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.app_theme_color1));
            mSecondStepLL.setVisibility(View.GONE);
            mFirstStepRightTV.setText("出示二维码");
            mFirstStepNameTV.setText("包裹回仓");
            mFirstStepTipTV.setVisibility(View.GONE);
            mFirstStepContentTV.setText("出示火箭侠二维码，交接包裹");
        } else {
            if (Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType()) {//驿站
                mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.app_color_ef4040));
                mSecondStepLL.setVisibility(View.GONE);
                mFirstStepRightTV.setText("扫描二维码");
                mFirstStepNameTV.setText("收包裹");
                mFirstStepTipTV.setVisibility(View.GONE);
                mFirstStepContentTV.setText("扫描驿站二维码，揽收包裹");
            } else if (Const.CourierTaskStatus.cabinetTask == serviceTaskBean.getTaskType()) {//快递柜
                mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.app_color_ef4040));
                mSecondStepLL.setVisibility(View.GONE);
                mFirstStepRightTV.setText("揽收");
                mFirstStepNameTV.setText("验收包裹");
                mFirstStepTipTV.setVisibility(View.GONE);
                mFirstStepContentTV.setText("在距快递柜200米内才可点击");
            } else {//上门
                mSecondStepLL.setVisibility(View.VISIBLE);
                if (Const.CourierTaskFinishStatus.contractSender == bean.getStepType()) {//第一步进行中
                    mFirstStepImageIV.setImageResource(R.drawable.task_first_step_red_icon);
                    mSecondStepImageIV.setImageResource(R.drawable.task_second_step_gray_icon);
                    mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.app_color_ef4040));
                    mSecondStepLL.setBackgroundColor(getResources().getColor(R.color.white));
                    mFirstStepTipTV.setVisibility(View.VISIBLE);
                    mFirstStepNameTV.setTextColor(getResources().getColor(R.color.white));
                    mFirstStepTipTV.setTextColor(getResources().getColor(R.color.white));
                    mFirstStepContentTV.setTextColor(getResources().getColor(R.color.white));
                    mFirstStepRightTV.setTextColor(getResources().getColor(R.color.white));
                    mFirstStepRightTV.setBackgroundResource(R.drawable.task_contact_sender_phone_e84f4f_bg);
                    mSecondStepNameTV.setTextColor(getResources().getColor(R.color.task_unfinish_color));
                    mSecondStepTipTV.setTextColor(getResources().getColor(R.color.task_unfinish_color));
                    mSecondStepContentTV.setTextColor(getResources().getColor(R.color.task_unfinish_color));
                    mSecondStepRightTV1.setTextColor(getResources().getColor(R.color.task_unfinish_color));
                    mSecondStepRightTV1.setBackgroundResource(R.drawable.task_contact_sender_phone_d7d7d7_bg);
                    mSecondStepRightTV2.setTextColor(getResources().getColor(R.color.task_unfinish_color));
                    mSecondStepRightTV2.setBackgroundResource(R.drawable.task_contact_sender_phone_d7d7d7_bg);
                    mFirstStepRightTV.setText("联系寄件人");
                    mFirstStepNameTV.setText("上门确认");
                    mFirstStepTipTV.setText("(第一步）");
                    mFirstStepContentTV.setText("请在通话中确认寄件人地址与时间");
                    mSecondStepNameTV.setText("验收包裹");
                    mSecondStepTipTV.setText("(第二步）");
                    mSecondStepContentTV.setText("根据实际情况选择验证方式");
                } else if (Const.CourierTaskFinishStatus.confirmParcelStatus == bean.getStepType()) {//第二步进行中
                    mFirstStepImageIV.setImageResource(R.drawable.task_finish_green_icon);
                    mSecondStepImageIV.setImageResource(R.drawable.task_second_step_red_icon);
                    mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.white));
                    mSecondStepLL.setBackgroundColor(getResources().getColor(R.color.app_color_ef4040));
                    mFirstStepTipTV.setVisibility(View.VISIBLE);
                    mFirstStepNameTV.setTextColor(getResources().getColor(R.color.task_finish_color));
                    mFirstStepTipTV.setTextColor(getResources().getColor(R.color.task_finish_color));
                    mFirstStepContentTV.setTextColor(getResources().getColor(R.color.task_finish_color));
                    mFirstStepRightTV.setTextColor(getResources().getColor(R.color.app_color_ef4040));
                    mFirstStepRightTV.setBackgroundResource(R.drawable.task_ef4040_radius3_shape);
                    mSecondStepNameTV.setTextColor(getResources().getColor(R.color.white));
                    mSecondStepTipTV.setTextColor(getResources().getColor(R.color.white));
                    mSecondStepContentTV.setTextColor(getResources().getColor(R.color.white));
                    mSecondStepRightTV1.setTextColor(getResources().getColor(R.color.white));
                    mSecondStepRightTV1.setBackgroundResource(R.drawable.task_contact_sender_phone_e84f4f_bg);
                    mSecondStepRightTV2.setTextColor(getResources().getColor(R.color.white));
                    mSecondStepRightTV2.setBackgroundResource(R.drawable.task_contact_sender_phone_e84f4f_bg);
                    mFirstStepRightTV.setText("联系寄件人");
                    mFirstStepNameTV.setText("上门确认");
                    mFirstStepTipTV.setText("(第一步）");
                    mFirstStepContentTV.setText("请在通话中确认寄件人地址与时间");
                    mSecondStepNameTV.setText("验收包裹");
                    mSecondStepTipTV.setText("(第二步）");
                    mSecondStepContentTV.setText("根据实际情况选择验证方式");
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (null == serviceTaskBean) {
            return;
        }
        Intent intent = null;
        switch (v.getId()) {
            //跳转到地图
            case R.id.map_tv:
                if (serviceTaskBean != null && serviceTaskBean.getLatitude() > 0 && serviceTaskBean.getLongitude() > 0) {
                    intent = new Intent(getActivityContext(), BaiDuMapActivity.class);
                    intent.putExtra("lat", serviceTaskBean.getLatitude());
                    intent.putExtra("lon", serviceTaskBean.getLongitude());
                    intent.putExtra("gdLat", serviceTaskBean.getGdLatitude());
                    intent.putExtra("gdLon", serviceTaskBean.getGdLongitude());
                    intent.putExtra("taskType", serviceTaskBean.getTaskType());
                    intent.putExtra("type", 0);
                    startActivity(intent);
                }
                break;
            //跳转到地图导航
            case R.id.navigation_tv:
                DialogMgr.showSelectMapToNavigation(getActivityContext(), findViewById(R.id.navigation_tv), serviceTaskBean.getLatitude(), serviceTaskBean.getLongitude(), serviceTaskBean.getGdLatitude(), serviceTaskBean.getGdLongitude());
                break;
            //拨打寄件人电话
            case R.id.sender_phone_tv:
                if (Const.CourierTaskStatus.visitTask == serviceTaskBean.getTaskType()) {
                    isCallPhone = true;
                    showLoadingDialog(null);
                    AppHttp.getInstance().updateTaskStepContractData(serviceTaskBean.getTaskId());
                } else {
                    Intents.dialOrCall(serviceTaskBean.getPhone());
                }
                break;
            //第一步右边操作 上门：联系寄件人  回仓：出示二维码  驿站揽收：扫描二维码  快递柜取件：揽收
            case R.id.first_step_right_tv:
                //上门第一步操作是联系寄件人
                if (Const.CourierTaskStatus.visitTask == serviceTaskBean.getTaskType()) {
                    isCallPhone = true;
                    showLoadingDialog(null);
                    AppHttp.getInstance().updateTaskStepContractData(serviceTaskBean.getTaskId());
                } else if (Const.CourierTaskStatus.transferTask == serviceTaskBean.getTaskType()) {//回仓交接 出示二维码
                    startActivity(CourierQRCodeActivity.class);
                } else if (Const.CourierTaskStatus.cabinetTask == serviceTaskBean.getTaskType()) {//快递柜揽收包裹
                    if (null == mBdLocation) {
                        return;
                    }
                    showLoadingDialog(null);
                    AppHttp.getInstance().courierCabinetTakeParcelData(serviceTaskBean.getTaskId(), mBdLocation.getLatitude(), mBdLocation.getLongitude());
                } else if (Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType()) {//驿站 扫描二维码
                    intent = new Intent(getActivityContext(), TaskCaptureActivity.class);
                    intent.setAction(Intents.courier_scan_for_task);
                    startActivityForResult(intent, Const.RequestCode.courier_scan_task);
                }
                break;
            //第二步 扫描二维码，只有上门取件才会有扫描二维码
            case R.id.scan_qrcode_tv:
                if (stageBean != null && Const.CourierTaskFinishStatus.confirmParcelStatus == stageBean.getStepType()) {
                    intent = new Intent(getActivityContext(), TaskCaptureActivity.class);
                    intent.setAction(Intents.courier_scan_for_task);
                    startActivityForResult(intent, Const.RequestCode.courier_scan_task);
                } else {
                    showToast("请先进行第一步操作");
                }
                break;
            //第二步  验证寄件码，只有上门取件才会有扫描二维码
            case R.id.confirm_send_code_tv:
                if (stageBean != null && Const.CourierTaskFinishStatus.confirmParcelStatus == stageBean.getStepType()) {
//                    intent = new Intent(getActivityContext(), ConfirmSendCodeDialogActivity.class);
//                    intent.putExtra("type", 0);
//                    intent.putExtra("item", serviceTaskBean);
//                    startActivityForResult(intent, 101);
                    DialogMgr.confirmSendCodeDialog(getActivityContext(), serviceTaskBean, 0);
                } else {
                    showToast("请先进行第一步操作");
                }
                break;
            //查看附近交接点
            case R.id.all_transfer_tv:
                startActivity(ParcelTransferListActivity.class);
                break;
            case R.id.sender_station_tv:
                if (serviceTaskBean == null) {
                    return;
                }
                DialogMgr.stationShow(getActivityContext(), serviceTaskBean);
                break;
        }
    }

    private void remaintimeCountdown(TextView countDown, long remainTime) {
        String[] diffs = AppLogic.timeDiffStr(remainTime);
        String timeText = "";
        if (diffs.length == 0) {
            timeText = "00小时00分";
        } else if (diffs.length == 1) {
            timeText = "00小时00分";
        } else if (diffs.length == 2) {
            timeText = "00小时" + diffs[0] + "分";
        } else if (diffs.length == 3) {
            timeText = diffs[0] + "小时" + diffs[1] + "分";
        }
        int sencondStart = timeText.indexOf(":");
        SpannableString secondSp = new SpannableString(timeText);
        secondSp.setSpan(new TextAppearanceSpan(getActivityContext(), R.style.task_count_down_style), sencondStart + 1, timeText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        countDown.setText("剩余时间：" + secondSp, TextView.BufferType.SPANNABLE);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //快递柜需要定位信息
            case MsgID.courier_task_stage_location_data:
                mBdLocation = (BDLocation) msg.obj;
                return true;
            //当前任务阶段操作步骤数据返回
            case MsgID.courier_task_step_detail:
                cancelLoadingDialog();
                final ReqResult<TaskStageBean> reqResult = JSON.parser(msg.obj, TaskStageBean.class);
                if (checkLoginStatus(reqResult)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            stageBean = reqResult.getResult();
                            if (stageBean == null) {
                                return;
                            }
                            updateViewForStep(stageBean);
                        }
                    });
                } else {
                    showToast(reqResult.getMessage());
                }
                return true;
            //当前任务阶段操作步骤进行更新返回
            case MsgID.courier_task_step_update:
                cancelLoadingDialog();
                final ReqResult<TaskStageBean> result = JSON.parser(msg.obj, TaskStageBean.class);
                if (checkLoginStatus(result)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            stageBean = result.getResult();
                            if (stageBean == null) {
                                return;
                            }
                            if (isCallPhone) {
                                DialogMgr.receiverOrSendCall(getActivityContext(), serviceTaskBean.getPhone(), 3, serviceTaskBean.getAddress(), null);
                            }
                            isCallPhone = false;
                            updateViewForStep(stageBean);
                        }
                    });
                } else {
                    isCallPhone = false;
                    showToast(result.getMessage());
                }
                return true;
            //返回包裹列表数据
            case MsgID.courier_scan_user_qrcode_data://扫描用户或驿站二维码
            case MsgID.courier_check_code_data://寄件码验证
            case MsgID.courier_cabinet_take_parcel_data://快递柜揽收
                cancelLoadingDialog();
                Android.hideSoftInput(getActivityContext());
                ReqResult<NewParcelBean> parcelResult = JSON.parseList(msg.obj, NewParcelBean.class);
                if (checkLoginStatus(parcelResult)) {
                    ArrayList<NewParcelBean> parcelList = parcelResult.getResultList();
                    if (parcelList != null) {
                        Intent intent = new Intent(getActivityContext(), ParcelListActivity.class);
                        intent.putExtra("items", parcelList);
                        startActivity(intent);
                    }
                } else {
                    showToast(parcelResult.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //扫描二维码返回的数据
        if (resultCode == Activity.RESULT_OK && Const.RequestCode.courier_scan_task == requestCode) {
            String result = data.getStringExtra("result");
            if (StringUtils.isEmpty(result)) {
                return;
            }
            showLoadingDialog(null);
            AppHttp.getInstance().scanUserQrCodeData(result, serviceTaskBean.getTaskId());
        }
        //上门验证寄件码返回
        else if (resultCode == Activity.RESULT_OK && 101 == requestCode) {
            String code = data.getStringExtra("code");
            showLoadingDialog(null);
            AppHttp.getInstance().confirmSendCodeData(code, serviceTaskBean.getTaskId());
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 获取验证码
     */
    public void setSendCode(String code) {
        showLoadingDialog(null);
        AppHttp.getInstance().confirmSendCodeData(code, serviceTaskBean.getTaskId());
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
        }
        super.onDestroy();
    }
}
